﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using IdentityModel.Client;
using Mr.Wang.IdentityServerModelPostting;

namespace ConsoleApp
{
    class Program
    {
        //模拟第三方客户端来 来获取access—tocken 和访问限定资源
        static void Main(string[] args)
        {


            #region 原始请求方式
            ////安装nuget包 IdentityModel

            ////2 创建一个httpclient
            //HttpClient client = new HttpClient();

            ////3 使用identityModel 为httpclient 写的拓展方法
            //var dios = client.GetDiscoveryDocumentAsync("http://localhost:5000").Result;
            //if (dios.IsError)
            //{
            //    Console.WriteLine(dios.Error);
            //    return;
            //}

            ////4 构建tockenClient
            //client.DefaultRequestHeaders.Accept.Clear();
            //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

            //var c = client.RequestTokenAsync(new TokenRequest()
            //{
            //    Address = dios.TokenEndpoint,
            //    ClientId = "android",//ConfigurationManager.AppSettings["ClientId"],
            //    ClientSecret = "12345", //ConfigurationManager.AppSettings["ClientSecret"],
            //    GrantType = "client_credentials", //ConfigurationManager.AppSettings["GrantType"],
            //    Parameters =
            //    {
            //        { "scope", "sendEmail" },
            //    }

            //}).GetAwaiter();
            //var r = c.GetResult();
            //if (r.IsError)
            //{
            //    throw new Exception(r.Error);
            //}
            //string timeStr = r.TryGet("expires_in");
            //Console.WriteLine(r.Json);
            ////带着token 请求需要认证服务
            //client.SetBearerToken(r.AccessToken);
            //HttpResponseMessage responseMgs = null;
            //try
            //{
            //    responseMgs = client.GetAsync("http://localhost:6001/weatherforecast").Result;
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.StackTrace);
            //    return;
            //}

            //if (responseMgs.IsSuccessStatusCode)
            //{
            //    Console.WriteLine(responseMgs.Content.ReadAsStringAsync().Result);
            //}
            //else
            //{
            //    Console.WriteLine(responseMgs.StatusCode);
            //}
            //client.Dispose();
            #endregion

            //string destUrl = "http://localhost:6001/weatherforecast";
            //string identityUrl = "http://localhost:5000";
            //TokenRequest tokenPar = new TokenRequest()
            //{
            //    ClientId = "android",
            //    ClientSecret = "12345",
            //    GrantType = "client_credentials",
            //    Parameters =     {
            //                                 { "scope", "sendEmail" },
            //                             },

            //};

            //for (int i = 0; i < 3; i++)
            //{
            //    string str = IdentityServerModelPostting.RequestGetAsync(destUrl,tokenPar, identityUrl).Result;
            //    Console.WriteLine(str);
            //}



            #region 带密码的请求方式



            string destUrl = "http://localhost:6001/weatherforecast";
            string identityUrl = "http://localhost:5000";
            var tokenPar = new PasswordTokenRequest
            {
                // Address = disco.TokenEndpoint,
                //下面2个属性对应的是 IdentityServer定义的测试用户，这里应是 Action 参数传递进来的，为了方便直接写死的
                UserName = "wz",
                Password = "wz123",
                //下面3个属性对应的是 IdentityServer定义的客户端
                ClientId = "ios",
                //ClientSecret = "",
                //Scope = "iosApiResource",
            };

            for (int i = 0; i < 3; i++)
            {
                string str = IdentityServerModelPostting.RequestGetAsync(destUrl, tokenPar, identityUrl).Result;
                Console.WriteLine(str);
            }

            

            #endregion



            Console.ReadKey();
        }
    }
}
